package com.qf.dao.impl;

import com.qf.dao.UserDao;
import com.qf.pojo.User;
import com.qf.utils.DruidUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

/**
 * @program: myproject
 * @description:
 * @author: mrsheng
 * @create: 2022-09-26 17:30
 **/


public class UserDaoImpl implements UserDao {
    private JdbcTemplate jdbcTemplate = new JdbcTemplate(DruidUtil.getDataSource());



        //查询所有
        @Override
        public List<User> findAll() {
            List<User> query = jdbcTemplate.query("select * from user", new BeanPropertyRowMapper<>(User.class));
            return query;
        }

    //登录


    @Override
    public User login(String username, String password) {
        List<User> query = jdbcTemplate.query("select * from user where username = ? and password =?",
                new BeanPropertyRowMapper<>(User.class), username, password);
        if (query!=null&&query.size()==1){
            return query.get(0);
        }
        return null;
    }

    //添加


    @Override
    public int addUser(User user) {
       String sql="insert into user values(null,?,?,?,?,?,?,?)";
        int update = jdbcTemplate.update(sql, user.getUsername(), user.getPassword(),
                user.getGender(), user.getAge(), user.getAddress(), user.getQq(), user.getEmail());
        return update;
    }

    //通过姓名查询


    @Override
    public List<User> findByNmame(String name) {
        List<User> query = jdbcTemplate.query("select * from user where username like ? ", new BeanPropertyRowMapper<>(User.class), "%"+name+"%");
        return query;
    }


    //修改


    @Override
    public int update(User user) {
        int update = jdbcTemplate.update(
                "update user set password=?,gender=?,age=?,address=?,qq=?,email=? where id = ?",
                user.getPassword(), user.getGender(),user.getAge(),user.getAddress(),user.getQq(),
                user.getEmail(),user.getId());
        return update;
    }


    //通过id查询
    @Override
    public User findUserById(int i) {
        String sql = "select * from user where id =  ?";
        List<User> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class), i);
        if (query!=null&&query.size()==1){
            return query.get(0);
        }
        return null;
    }

    @Override
    public int deleteUser(int id) {
        int update = jdbcTemplate.update("delete from user where id=?", id);
        return update;
    }

    //分页查询
    @Override
    public List<User> findAll(int i, int pageSize) {
        List<User> query = jdbcTemplate.query("select * from user limit ? , ?", new BeanPropertyRowMapper<>(User.class), i, pageSize);
        return query;
    }

    //总记录数

    @Override
    public int count() {
        int integer = jdbcTemplate.queryForObject("select count(*) from user", Integer.class);
            return integer;
    }



   //通过名字模糊查询并分页


    @Override
    public List<User> findAll(int i, int pageSize, String username) {
        List<User> query = jdbcTemplate.query(
                "select * from user where username like ? limit ?,? ", new BeanPropertyRowMapper<>(User.class),
                "%"+username+"%",i,pageSize);
        return query;

    }

    @Override
    public int count(String username) {
        int integer = jdbcTemplate.queryForObject("select count(*) from user where username like ?", Integer.class,"%"+username+"%");
        return integer;
    }
}

